//
//  MULLoginManager.h
//  UniLoginSDK
//
//  Created by Song Xiaofeng on 13-11-27.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MULThirdPartInfo.h"
#import "MULMBProgressHUD.h"

#define kMULLoginErrorDomain @"LoginError"
#define kMULLoginErrorMessage @"errorMsg"
#define kMULUserDidLogin @"MULUserDidLogin"
#define kMULUserDidLogout @"MULUserDidLogout"

extern NSString *kLoginURL;
extern NSString *kRegisterURL;
extern NSString *k3rdPartLoginURL;

typedef enum
{
    kLoginOpt_Login = 1,
    kLoginOpt_Reg,
    kLoginOpt_3rdLogin,
    
    kLoginOpt_DefaultLogin,
    kLoginOpt_PhoneLogin
}MULLoginOperationType;

typedef void (^LoginFinished)(BOOL success, NSDictionary *baseInfo, NSDictionary *extraInfo, NSError *error);
typedef void (^DidStartBind)();
typedef void (^NavigationConfigBlock)(UINavigationController *navigationController);

@class MULXAuthInfo;
@interface MULLoginManager : NSObject
{
    
}
@property(nonatomic, readonly)BOOL hasLogin;// has been logined or not
@property(nonatomic, copy)NavigationConfigBlock bindNavigationConfigBlock;

+ (MULMBProgressHUD*)showTextTips:(NSString*)textTips inView:(UIView*)parentView;

+ (MULLoginManager*)sharedInstance;

/**
 *  set XAuth consumerkey which identify the unique
 *
 *  @param consumerKey
 *  @param consumerSecret
 *
 *  @return MULLoginManager class, so you could write contiuous setter methods of class-methods
 */
+ (Class)startWithXAuthConsumerKey:(NSString*)consumerKey andConsumerSecret:(NSString*)consumerSecret;

/**
 *  set XAuth default token and token secret, which used for non-login reuqests
 *
 *  @param xauthToken
 *  @param tokenSecret
 *
 *  @return
 */
+ (Class)setXAuthDefaultToken:(NSString *)xauthToken andSecret:(NSString*)tokenSecret;

+ (void)getSignatureToken:(NSString**)token andSecret:(NSString**)secret;
+ (void)getConsumerKey:(NSString**)consumerKey andConsumerSecret:(NSString**)consumerSecret;

+ (MULXAuthInfo*)xauthInfo;


#pragma mark - Login actions
- (BOOL)loginWithPhoneNumber:(NSString *)number
                     message:(NSString *)message
               finishedBlock:(LoginFinished)finishedBlock;

- (BOOL)loginWithMFWAccount:(NSString*)account
                   password:(NSString*)password
              finishedBlock:(LoginFinished)finishedBlock;

- (BOOL)loginWithMFWAccount:(NSString*)account
                   password:(NSString*)password
                   openUDID:(NSString*)openUDID
              finishedBlock:(LoginFinished)finiihedBlock;

//-----------------------------------------------------------------
- (BOOL)registerWithMFWAccount:(NSString*)account
                      password:(NSString*)password
                      nickName:(NSString*)nickName
                 finishedBlock:(LoginFinished)finishedBlock;

- (BOOL)registerWithMFWAccount:(NSString*)account
                      password:(NSString*)password
                      nickName:(NSString*)nickName
                      openUDID:(NSString*)openUDID
                         email:(NSString*)email
                 finishedBlock:(LoginFinished)finishedBlock;

- (BOOL)loginWith3rdUid:(NSString*)userid
                   type:(NSString*)type
                   name:(NSString*)name
                 header:(NSString*)header
               location:(NSString*)location
                 gender:(NSString*)gender
                unionId:(NSString*)unionId
          finishedBlock:(LoginFinished)finishedBlock;

//-----------------------------------------------------------------

/**
 * @see
 - (BOOL)loginWith3rdPartAppKey:(NSString*)appkey
                clientAccessToken:(NSString*)clientAccessToken
                     wechatUniqid:(NSString*)uniqid
                     sourceUID:(NSString*)sourceUID
                     expiredDate:(NSDate*)expiredDate
                     thirdPartType:(MULLoginThirdPartType)type
                     nickName:(NSString*)nickName
                     logoUrl:(NSString*)logoUrl
                     startBindBlock:(DidStartBind)startBindBlock
                     finishedBlock:(LoginFinished)finishedBlock;
 */
- (BOOL)loginWith3rdPartAppKey:(NSString*)appkey
             clientAccessToken:(NSString*)clientAccessToken
                     sourceUID:(NSString*)sourceUID
                   expiredDate:(NSDate*)expiredDate
                 thirdPartType:(MULLoginThirdPartType)type
                      nickName:(NSString*)nickName
                       logoUrl:(NSString*)logoUrl
                startBindBlock:(DidStartBind)startBindBlock
                 finishedBlock:(LoginFinished)finishedBlock;

/**
 *  第三方登录
 *
 *  @param appkey
 *  @param clientAccessToken
 *  @param uniqid  微信登录时用
 *  @param sourceUID
 *  @param expiredDate
 *  @param type
 *  @param nickName
 *  @param logoUrl
 *  @param startBindBlock
 *  @param finishedBlock
 *
 *  @return
 */
- (BOOL)loginWith3rdPartAppKey:(NSString*)appkey
             clientAccessToken:(NSString*)clientAccessToken
                  wechatUniqid:(NSString*)uniqid
                     sourceUID:(NSString*)sourceUID
                   expiredDate:(NSDate*)expiredDate
                 thirdPartType:(MULLoginThirdPartType)type
                      nickName:(NSString*)nickName
                       logoUrl:(NSString*)logoUrl
                      openUDID:(NSString*)openUDID
                startBindBlock:(DidStartBind)startBindBlock
                 finishedBlock:(LoginFinished)finishedBlock;

#pragma mark -
/**
 * 用于已登录用户应用的无缝集成，将已存在的token更新到UniLoginSDK管理
 *
 *  @param token  access token
 *  @param secret token secret
 *  @param uid    uid
 *
 *  @return Success or not
 */
- (BOOL)updateAccessToken:(NSString*)token andSecret:(NSString*)secret forUid:(NSString*)uid;

- (BOOL)isRequestSending;

- (void)logout;

- (void)cancelLoginOrRegistration;


-(BOOL)setCookie:(NSDictionary*)cookies;

#pragma mark - refresh default user info
- (void)refreshDefaultToken;
@end
